﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using Lswweb.Model;
using Lswweb.Common;

namespace Lswweb.Domain
{
    public class Email
    {
        public static void SendMail(string sendEmail, string titleInnerHTML, string bodyInnerHTML){
            SendMail(sendEmail, titleInnerHTML, bodyInnerHTML, null);
        }
        public static void SendMail(string sendEmail, string titleInnerHTML, string bodyInnerHTML,params string[] bccGroup)
        {
            string smtp = SiteSettings.Settings.SmtpServer;
            string fromEmail = SiteSettings.Settings.SmtpEmail;
            string emailUser = SiteSettings.Settings.SmtpUser;
            string emailPsw = SiteSettings.Settings.SmptPsw;

            if (string.IsNullOrEmpty(smtp) || string.IsNullOrEmpty(sendEmail) || string.IsNullOrEmpty(emailUser) || string.IsNullOrEmpty(emailPsw))
                throw new Exception("邮件服务器配置错误，发送的邮件信息不完整。");

            MailMessage msg = new MailMessage();
            msg.To.Add(sendEmail);
            msg.From = new MailAddress(fromEmail);
            msg.Subject = titleInnerHTML;//邮件标题 
            msg.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件标题编码 
            msg.Body = bodyInnerHTML;//邮件内容 
            msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码 
            msg.IsBodyHtml = true;//是否是HTML邮件 
            msg.Priority = MailPriority.High;//邮件优先级 

            bccGroup.ForEach(info=>msg.Bcc.Add(info));

            SmtpClient client = new SmtpClient(smtp);
            client.Credentials = new NetworkCredential(emailUser, emailPsw);

            try
            {
                client.Send(msg);

                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            catch
            {
                SiteContext.RenderMessage(ExceptionType.SendMailError);
            }
        }

        /// <summary>
        /// 注册新用户
        /// </summary>
        /// <param name="user"></param>
        public static void SendMail(UserInfo user)
        {
            SinglePageInfo info = SinglePages.GetSinglePage(SinglePage.UserRegisterMail);
            if (info == null)
                return;

            string titleInnerHTML=info.Title;
            string bodyInnerHTML = info.View.Replace("[UserName]", user.LoginName).Replace("[UserPassword]", user.Password);

            SendMail(user.Email, titleInnerHTML, bodyInnerHTML);
        }

        /// <summary>
        /// 新订单
        /// </summary>
        /// <param name="order"></param>
        public static void SendMail(string email,OrderInfo order)
        {
            SinglePageInfo info = SinglePages.GetSinglePage(SinglePage.NewOrderMail);
            UserInfo user = Users.GetUser(order.UserId);
            if (info == null || user==null)
                return;

            Ue.Infrastructure.Web.UI.RenderViewControl<System.Web.UI.UserControl> control = new Ue.Infrastructure.Web.UI.RenderViewControl<System.Web.UI.UserControl>(Globals.GetSkinPath("/Skins/Order/OrderShow2.ascx"));
            control.SkinControl.SetPropertyValue("OrderId", order.Id);

            string titleInnerHTML = info.Title;
            string bodyInnerHTML = info.View
                .Replace("[OrderInfo]", control.RenderControl())
                .Replace("[UserName]", (user.Profile != null && !string.IsNullOrEmpty(user.Profile.Name)) ? user.Profile.Name : user.LoginName)
                .Replace("[OrderNo]", order.Id.ToString())
                .Replace("[CreateOn]", order.DateCreated.ToString());

            SendMail(email, titleInnerHTML, bodyInnerHTML);
        }

        public static void SendMailGetPassword(string email, string userName, string psw)
        {
            SinglePageInfo info = SinglePages.GetSinglePage(SinglePage.GetPasswordMail);
            if (info == null)
                return;

            string titleInnerHTML = info.Title;
            string bodyInnerHTML = info.View.Replace("[UserName]", userName).Replace("[UserPassword]", psw);

            SendMail(email, titleInnerHTML, bodyInnerHTML);
        }
    }
}
